//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartSecondaryAxisView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartSecondaryAxisView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartSecondaryAxisView

IMPLEMENT_DYNCREATE(CChartSecondaryAxisView, CBCGPChartExampleView)

CChartSecondaryAxisView::CChartSecondaryAxisView()
	: CBCGPChartExampleView(CChartSecondaryAxisView::IDD)
{
	//{{AFX_DATA_INIT(CChartSecondaryAxisView)
	m_bSecondaryX = TRUE;
	m_bSecondaryY = TRUE;
	//}}AFX_DATA_INIT
}

CChartSecondaryAxisView::~CChartSecondaryAxisView()
{
}

void CChartSecondaryAxisView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartSecondaryAxisView)
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_SHOW_SECONDARY_X, m_bSecondaryX);
	DDX_Check(pDX, IDC_SHOW_SECONDARY_Y, m_bSecondaryY);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartSecondaryAxisView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartSecondaryAxisView)
	ON_BN_CLICKED(IDC_SHOW_SECONDARY_X, OnUpdateChart)
	ON_BN_CLICKED(IDC_SHOW_SECONDARY_Y, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartSecondaryAxisView diagnostics

#ifdef _DEBUG
void CChartSecondaryAxisView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartSecondaryAxisView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartSecondaryAxisView message handlers

void CChartSecondaryAxisView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Series 1"));
	pSeries1->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE);

	CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Series 2"));
	pSeries2->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);

	pSeries1->AddDataPoint(0.);
	pSeries1->AddDataPoint(2.);
	pSeries1->AddDataPoint(5.);
	pSeries1->AddDataPoint(3.);
	pSeries1->AddDataPoint(3.5);
	pSeries1->AddDataPoint(5.);
	pSeries1->AddDataPoint(8.);
	pSeries1->AddDataPoint(7.);

	pSeries2->AddDataPoint(2.);
	pSeries2->AddDataPoint(6.);
	pSeries2->AddDataPoint(12.);
	pSeries2->AddDataPoint(16.);
	pSeries2->AddDataPoint(14.);
	pSeries2->AddDataPoint(9.);
	pSeries2->AddDataPoint(6.);
	pSeries2->AddDataPoint(2.);

	pChart->ShowDataMarkers();
	pChart->ShowDataLabels();

	SetDefaultLineWidth();
	OnUpdateChart();
}

void CChartSecondaryAxisView::OnUpdateChart()
{
	CWaitCursor wait;

	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartSeries* pSeries2 = pChart->GetSeries(1);
	ASSERT_VALID(pSeries2);

	pSeries2->SetRelatedAxes(pChart->GetChartAxis(m_bSecondaryX ? BCGP_CHART_X_SECONDARY_AXIS : BCGP_CHART_X_PRIMARY_AXIS), 
							pChart->GetChartAxis(m_bSecondaryY ? BCGP_CHART_Y_SECONDARY_AXIS : BCGP_CHART_Y_PRIMARY_AXIS));

	CBCGPChartAxis* pXAxis1 = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
	ASSERT_VALID(pXAxis1);

	pXAxis1->m_strAxisName = m_bSecondaryX ? _T("Series 1 X") : _T("Series 1 and 2 X");
	pXAxis1->m_bDisplayAxisName = TRUE;
	pXAxis1->m_bVisible = false;

	CBCGPChartAxis* pXAxis2 = pChart->GetChartAxis(BCGP_CHART_X_SECONDARY_AXIS);
	ASSERT_VALID(pXAxis2);

	pXAxis2->m_strAxisName = _T("Series 2 X");
	pXAxis2->m_bDisplayAxisName = TRUE;

	CBCGPChartAxis* pYAxis1 = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
	ASSERT_VALID(pYAxis1);

	pYAxis1->m_strAxisName = m_bSecondaryY ? _T("Series 1 Y") : _T("Series 1 and 2 Y");
	pYAxis1->m_bDisplayAxisName = TRUE;

	CBCGPChartAxis* pYAxis2 = pChart->GetChartAxis(BCGP_CHART_Y_SECONDARY_AXIS);
	ASSERT_VALID(pYAxis2);

	pYAxis2->m_strAxisName = _T("Series 2 Y");
	pYAxis2->m_bDisplayAxisName = TRUE;

	pChart->SetDirty();
	pChart->Redraw();
}
